<template>
  <view class="container">
    <view class="content">
      <slot></slot>
    </view>
  </view>
</template>

<script setup>
// App.vue
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { useUserStore } from '@/stores'
import { message, system } from '@/utils'
let userStore

onLaunch(() => {
  userStore = useUserStore()
  // 获取系统信息
  const systemInfo = system.getSystemInfo()
  console.log('App Launch - System Info:', systemInfo)

  // 监听错误事件
  uni.onError((err) => {
    console.error('App Error:', err)
  })

  // 检查登录状态
  checkLoginStatus()
})

// 检查登录状态
const checkLoginStatus = async () => {
  try {
    // 获取当前页面路径
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]?.route || ''
    console.log('Current page:', currentPage)
    
    // 检查是否有token
    const token = uni.getStorageSync('token')
    
    // 初始化用户状态，确保恢复正确的登录状态或游客状态
    userStore.initUserState()
    
    // 符合微信小程序规范：不强制跳转到登录页
    // 用户可以直接浏览小程序内容，只有在需要用户身份的操作时才提示登录
    console.log('初始化用户状态完成，允许用户浏览内容')
    // 如果没有token，自动设置为游客模式
    if (!token) {
      userStore.setGuestMode()
    } else if (token && userStore) {
      // 如果有token，获取用户信息
      await userStore.getUserInfo()
    }
    
    console.log('用户状态初始化完成:', { 
      isLogin: userStore.isLogin, 
      isGuestMode: userStore.isGuestMode 
    })
  } catch (error) {
    console.error('Login check failed:', error)
    // 发生错误时，不自动设置为游客模式，而是跳转到登录页
    uni.reLaunch({
      url: '/pages/login/account-login'
    })
  }
}

onShow(() => {
  // 获取设备信息
  const deviceInfo = system.getDeviceInfo()
  console.log('App Show - Device Info:', deviceInfo)

  // 监听主题变化
  uni.onThemeChange(({ theme }) => {
    console.log('Theme changed:', theme)
  })
})

onHide(() => {
  console.log('App Hide')
})
</script>

<style lang="scss">
@import './common/style/mixins.scss';

.container {
  min-height: 100vh;
  background-color: $bg-color;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  transition: background-color 0.3s ease;

  .content {
    flex: 1;
    width: 100%;
    padding: 0 30rpx;
    box-sizing: border-box;
    position: relative;
    z-index: 1;

    // 全局页面切换动画
    &.page-enter-active,
    &.page-leave-active {
      transition: all 0.3s ease;
    }

    &.page-enter-from,
    &.page-leave-to {
      opacity: 0;
      transform: translateX(30rpx);
    }

    // 全局组件过渡动画
    .fade-enter-active,
    .fade-leave-active {
      transition: opacity 0.3s ease;
    }

    .fade-enter-from,
    .fade-leave-to {
      opacity: 0;
    }

    .slide-up-enter-active,
    .slide-up-leave-active {
      transition: all 0.3s ease-out;
    }

    .slide-up-enter-from,
    .slide-up-leave-to {
      transform: translateY(20rpx);
      opacity: 0;
    }
	
  }
}

// 全局主题变量
:root {
  --primary-color: #5677fc;
  --primary-light: rgba(86, 119, 252, 0.1);
  --text-color: #333;
  --text-color-secondary: #666;
  --text-color-light: #999;
  --border-color: #eee;
  --bg-color: #f8f9fa;
  --card-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  --transition-base: all 0.3s ease;
}
</style>